package com.ruoyi.quality.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.quality.domain.QualityPhysicalReport;
import com.ruoyi.quality.domain.QualityUserClassification;
import com.ruoyi.quality.domain.bo.QualityPhysicalReportBo;
import com.ruoyi.quality.domain.vo.QualityPhysicalReportVo;
import com.ruoyi.quality.mapper.QualityPhysicalReportMapper;
import com.ruoyi.quality.service.IQualityPhysicalReportService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 质量通报Service业务层处理
 *
 * @author ruoyi
 * @date 2023-07-31
 */
@RequiredArgsConstructor
@Service
public class QualityPhysicalReportServiceImpl implements IQualityPhysicalReportService {

    private final QualityPhysicalReportMapper baseMapper;

    /**
     * 查询质量通报
     */
    @Override
    public QualityPhysicalReportVo queryById(Long reportId){
        return baseMapper.selectVoById(reportId);
    }

    /**
     * 查询质量通报列表
     */
    @Override
    public TableDataInfo<QualityPhysicalReportVo> queryPageList(QualityPhysicalReportBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<QualityPhysicalReport> lqw = buildQueryWrapper(bo);
        Page<QualityPhysicalReportVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询最新质量通报号
     */
    @Override
    public Long getMaxNumber(QualityPhysicalReportBo bo) {
        Long result = null;
        Map<String, Object> params = bo.getParams();
        LocalDateTime firstDayOfYear = LocalDateTime.of(LocalDate.now().getYear(), 1, 1, 0, 0, 0);
        String formattedDateTime = firstDayOfYear.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        params.put("beginReportDate", formattedDateTime);
        LambdaQueryWrapper<QualityPhysicalReport> lqw = buildQueryWrapper(bo);
        QualityPhysicalReportVo vo = baseMapper.selectVoOne(lqw.orderByDesc(QualityPhysicalReport::getReportNumber).last("limit 1"));
        if (vo != null) {
            result = Long.valueOf(vo.getReportNumber());
        }
        return result;
    }

    /**
     * 查询质量通报列表
     */
    @Override
    public List<QualityPhysicalReportVo> queryList(QualityPhysicalReportBo bo) {
        LambdaQueryWrapper<QualityPhysicalReport> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<QualityPhysicalReport> buildQueryWrapper(QualityPhysicalReportBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<QualityPhysicalReport> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getReportNumber()), QualityPhysicalReport::getReportNumber, bo.getReportNumber());
        lqw.like(StringUtils.isNotBlank(bo.getReportName()), QualityPhysicalReport::getReportName, bo.getReportName());
        lqw.eq(StringUtils.isNotBlank(bo.getReportAbstract()), QualityPhysicalReport::getReportAbstract, bo.getReportAbstract());
        lqw.eq(StringUtils.isNotBlank(bo.getDeptAbbreviation()), QualityPhysicalReport::getDeptAbbreviation, bo.getDeptAbbreviation());
        lqw.eq(bo.getReportDate()!=null, QualityPhysicalReport::getReportDate, bo.getReportDate());
        lqw.orderByDesc(QualityPhysicalReport::getReportDate);
        lqw.between(params.get("beginReportDate") != null && params.get("endReportDate") != null,
                QualityPhysicalReport::getReportDate ,params.get("beginReportDate"), params.get("endReportDate"));
        lqw.eq(StringUtils.isNotBlank(bo.getReportMeasure()), QualityPhysicalReport::getReportMeasure, bo.getReportMeasure());
        return lqw;
    }

    /**
     * 新增质量通报
     */
    @Override
    public Boolean insertByBo(QualityPhysicalReportBo bo) {
        QualityPhysicalReport add = BeanUtil.toBean(bo, QualityPhysicalReport.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setReportId(add.getReportId());
        }
        return flag;
    }

    /**
     * 新增质量通报
     */
    @Override
    public Long insertByBo(QualityPhysicalReportBo bo,Boolean isValid) {
        QualityPhysicalReport add = BeanUtil.toBean(bo, QualityPhysicalReport.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        Long reportId = add.getReportId();
        if (flag) {
            bo.setReportId(reportId);
        }
        return reportId;
    }

    /**
     * 修改质量通报
     */
    @Override
    public Boolean updateByBo(QualityPhysicalReportBo bo) {
        QualityPhysicalReport update = BeanUtil.toBean(bo, QualityPhysicalReport.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(QualityPhysicalReport entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除质量通报
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
